package client.texture;

import org.lwjgl.opengl.GL11;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public abstract class TextureAtlas {

    public final int textureId;
    protected final Map<String, UV> textures = new HashMap<>();

    public TextureAtlas(int textureId) {
        this.textureId = textureId;
    }

    public void put(String name, UV uv) {
        if (textures.containsKey(name)) {
            throw new RuntimeException("Conflict texture name " + name);
        }
        textures.put(name, uv);
    }

    public Optional<UV> get(String name) {
        return Optional.ofNullable(textures.get(name));
    }

    public UV getOrMissing(String name) {
        return textures.getOrDefault(name, getDefaultMissingUV());
    }

    public void bind() {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
    }

    public void unbind() {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }

    protected abstract UV getDefaultMissingUV();
}
